<?php

namespace app\admin\model\chase;

use think\Model;
use traits\model\SoftDelete;

class Learning extends Model
{

    use SoftDelete;

    

    // 表名
    protected $table = 'learnings';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [

    ];
    
    public function knowledges()
    {
        return $this->hasMany(LearningImages::class, 'learning_id', 'id')->where('type', 'knowledge');
    }

    public function homeworks()
    {
        return $this->hasMany(LearningImages::class, 'learning_id', 'id')->where('type', 'homework');
    }

    public function student()
    {
        return $this->hasOne(Student::class, 'id', 'student_id');
    }

    public static function getByStudentId($student_id)
    {
        return self::whereTime('createtime', 'today')->where('student_id', $student_id)->find();
    }

    public function getCreatetimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', $value) : '';
    }

}
